﻿using AutoMapper;
using BasicInformation.ApiWrite.Application.Command.Depot;
using BasicInformation.Domain;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using MediatR;

namespace BasicInformation.ApiWrite.Application.Handler.Depot
{
    /// <summary>
    /// 修改仓库管理处理类
    /// </summary>
    public class UpdateDepotHandler : IRequestHandler<UpdateDepotCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DeleDepotHandler> logger;
        private readonly IMapper mapper;

        public UpdateDepotHandler(DatabaseContext db, ILogger<DeleDepotHandler> logger,IMapper mapper)
        {
            this.db = db;
            this.logger = logger;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateDepotCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var depot = db.SqlSugar.Queryable<DepotModel>().Where(x => x.DepotId == request.DepotId).First();
            depot = mapper.Map(request,depot);
            int r= await db.SqlSugar.Updateable(depot).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "仓库管理修改成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "仓库管理修改失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
